package com.fywsy.fyweb.repository;

import com.fywsy.fyweb.entity.Qufu;
import com.fywsy.fyweb.entity.SendLog;
import com.fywsy.fyweb.entity.projection.GameDailyStatsProjection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SendLogRepository extends JpaRepository<SendLog, Long> {

    /**
     * 统计游戏后台使用人数
     * day1:今天
     * day2:昨天
     * .....
     * @return
     */
    @Query(value = "SELECT " +
                   "game_id as gameId, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = CURRENT_DATE THEN uid END) as day1, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) THEN uid END) as day2, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 2 DAY) THEN uid END) as day3, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 3 DAY) THEN uid END) as day4, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 4 DAY) THEN uid END) as day5, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 5 DAY) THEN uid END) as day6, " +
                   "COUNT(DISTINCT CASE WHEN DATE(create_time) = DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY) THEN uid END) as day7 " +
                   "FROM send_log " +
                   "WHERE create_time >= DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY) " +
                   "GROUP BY game_id",
            nativeQuery = true)
    List<GameDailyStatsProjection> findGameDailyStatsNative();
}